package com.my.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.my.annotation.SystemOperatorLog;
import com.my.common.CommonBusiLogic;
import com.my.dao.EmployeeMapperExt;
import com.my.entity.Employee;
import com.my.entity.EmployeeExample;
import com.my.entity.Page;
import com.my.entity.ResponseData;
import com.my.service.EmployeeService;
import com.my.util.CookieUtil;
import com.my.util.PinYin4jUtil;
import com.sun.org.apache.xml.internal.security.utils.Base64;
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
	private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceImpl.class);

	@Resource
	private EmployeeMapperExt  employeeMapperExt;
	
	@Override
	public int deleteByPrimaryKey(Long id) {
		return employeeMapperExt.deleteByPrimaryKey(id);
	}
	@Override
	public int insertSelective(Employee record) {
		return employeeMapperExt.insertSelective(record);
	}
	@Override
//	@Cacheable(value="employee",key="#id")
	public Employee selectByPrimaryKey(Long id) {
		return employeeMapperExt.selectByPrimaryKey(id);
	}
	@Override
	public int updateByPrimaryKeySelective(Employee record) {
		return employeeMapperExt.updateByPrimaryKey(record);
	}

	@Override
	public Employee queryManagerEmployeeName(String name) {
		return employeeMapperExt.queryManagerEmployeeName(name);
	}
	@Override
	public Employee login(String name, String password) {
		Employee emp=employeeMapperExt.queryByName(name);
		String pwd=Base64.encode(CookieUtil.getMD5(password).getBytes());
		if(null!=emp && emp.getPassword().equals(pwd)){
			return emp;
		}else {
			//登录失败
			return null;
		}
	}
	@Override
	public Employee queryByName(String name) {
		return employeeMapperExt.queryByName(name);
	}

	@Override
	public int register(Employee employee,String updateEmp,String[] hobby){
		String hobbys="";
		for (String string : hobby) {
			hobbys+=string+",";
		}
		employee.setHobby(hobbys);
		employee.setAbbr(PinYin4jUtil.getPinYinHeadChar(employee.getRealName()).toUpperCase());
		employee.setRecUpdateEmp(updateEmp);
		if(employee.getSex()!=null && employee.getSex().equals("on")){
			employee.setSex("男");
		}else {
			employee.setSex("女");
		}
		//处理数据库明文密码
		employee.setPassword(Base64.encode(CookieUtil.getMD5(employee.getPassword()).getBytes()));
		return this.insertSelective(employee);
	}
	@Override
	public List<Employee> selectByExample(EmployeeExample example) {
		return employeeMapperExt.selectByExample(example);
	}
	
	
	@Override
	public ResponseData<Employee> selectAllEmpForPage(Page page) {
		if(null==page || page.getCurrent()==0 || page.getLength()==0){
			page=new Page();
			page.setCurrent(1);
			page.setLength(10);
		}
		page = CommonBusiLogic.setPageBeginByPage(page);
		List<Employee> emps=employeeMapperExt.queryForPage(page);
		int count=employeeMapperExt.countQueryForPage();
		ResponseData<Employee> responseData=new ResponseData<Employee>(emps, count);
		return responseData;
	}
	
	@Override
	public Employee queryDetailById(Long id) {
		return employeeMapperExt.queryDetailById(id);
	}
	@Override
	public List<Employee> queryIsDeleteEmp() {
		return employeeMapperExt.queryIsDeleteEmp();
	}

}
